package org.apache.ocean;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.lucene.index.Term;
import org.jdom.Element;

/**
 * 
 * @author jasonr
 */
public class TermFrequencies extends HashMap<Term, AtomicLong> {
	public TermFrequencies() {
	}
  
	public void add(Term term, AtomicLong value) {
		AtomicLong currentValue = get(term);
		if (currentValue == null) {
			put(term, new AtomicLong(value.get()));
		} else {
			currentValue.addAndGet(value.get());
		}
	}
	
	public TermFrequencies(int size) {
		super(size);
	}

	public TermFrequencies(List<Element> elements) {
		for (Element element : elements) {
			String field = XMLUtil.getChildText("field", element);
			String text = XMLUtil.getChildText("text", element);
			CLong frequency = XMLUtil.getChildLong("frequency", element);
			Term term = new Term(field, text);
			put(term, new AtomicLong(frequency.value));
		}
	}
}
